package com.example.cyw.news;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

/**
 * Created by cyw on 2018/5/19.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private News news;
    private Context mContext;
    private Jump jump;
    public MyAdapter(Context mContext,News news,Jump jump){
        this.news=news;
        this.mContext=mContext;
        this.jump=jump;
        Log.e("myadapter","构造函数");
        if (news!=null) {
            Log.e("myadapetr", "size" + news.getResult().getData().size());
        }

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.e("ada","1");
       View view= LayoutInflater.from(mContext).inflate(R.layout.news_list_item,parent,false);
        final MyViewHolder viewHolder=new MyViewHolder(view);
        viewHolder.NewsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position=viewHolder.getAdapterPosition();
                String url=news.getResult().getData().get(position).getUrl();
               jump.start(url);

            }
        });
        return  viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.e("ada","1");
        News.ResultBean.DataBean dataBean=news.getResult().getData().get(position);
        Log.e("ada",dataBean.getTitle());
        holder.tvtitle.setText(dataBean.getTitle());
        holder.tvAuthor.setText(dataBean.getAuthor_name());
        holder.tvTimer.setText(dataBean.getDate());


        Glide.with(mContext).load(Uri.parse(dataBean.getThumbnail_pic_s())).into(holder.newsHeadImg);

    }


    @Override
    public int getItemCount() {
        Log.e("getItemCount","被调用");
       if (news==null){
           return 0;
       }
        Log.e("size",""+news.getResult().getData().size());
            return news.getResult().getData().size();



    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        View NewsView;
        ImageView newsHeadImg;
        TextView tvtitle;
        TextView tvAuthor;
        TextView tvTimer;

        public MyViewHolder(View itemView) {
            super(itemView);
            NewsView=itemView;
            newsHeadImg=itemView.findViewById(R.id.iv_head);
            tvtitle=itemView.findViewById(R.id.iv_title);
            tvAuthor=itemView.findViewById(R.id.iv_author);
            tvTimer=itemView.findViewById(R.id.iv_time);
        }
    }
}
